<?php
/*
	bestand:		index.php
	taak:			Backend interface voor compare van mySql data

*/

ini_set ('error_reporting', E_ALL | E_STRICT | E_NOTICE);

require_once 'config/server.inc.php';
require_once 'config/class/backuprestore.class.php';
require_once 'config/class/compare.class.php';
require_once 'config/class/sync.class.php';
include_once("inc/functions.inc.php");


// =================================================================================
//  Comapere Databases
// =================================================================================
if (isset($_GET['get']) && $_GET['get']=='comparedb') {

	$table =  isset($_GET['table']) ? $_GET['table'] : "";
	$srvdba = (isset($_GET['dba'])) ? $_GET['dba'] : "";
	$srvdbb = (isset($_GET['dbb'])) ? $_GET['dbb'] : "";
	if ($srvdba==$srvdbb) exit("Must compare 2 different databases");
	
	$compare = new COMPARE($srv, $srvdba, $srvdbb, QUIET);
	$obj = $compare->comparedb($table);
	unset($compare); // Destroy object, otherwise output-buffer is in affect
	
	echo json_encode($obj);
	exit;	
}
// =================================================================================

// =================================================================================
//  Compare Table
// =================================================================================
if (isset($_GET['get']) && $_GET['get']=='deepcompare') {

	$srvdba = (isset($_GET['dba'])) ? $_GET['dba'] : "";
	$srvdbb = (isset($_GET['dbb'])) ? $_GET['dbb'] : "";
	$table  = (isset($_GET['table'])) ? $_GET['table'] : "";
	$keys   = (isset($_GET['keys'])) ? explode(",", $_GET['keys']) : array();

	$compare = new COMPARE($srv, $srvdba, $srvdbb, QUIET);
	if (isset($_GET['filter'])) $compare->setTblFilter($_GET['filter']); // Stel propertie in om filter te bepalen
	$obj = $compare->deepcompare($table, $keys);
	unset($compare); // Destroy object, otherwise output-buffer is in affect

	echo json_encode($obj);
	exit;
	}
// =================================================================================


// =================================================================================
//  Compare Row
// =================================================================================
if (isset($_GET['get']) && $_GET['get']=='rowcompare') {

	$srvdba = (isset($_GET['dba'])) ? $_GET['dba'] : "";
	$srvdbb = (isset($_GET['dbb'])) ? $_GET['dbb'] : "";
	$table  = (isset($_GET['table'])) ? $_GET['table'] : "";
	$rowsel = (isset($_GET['rowsel'])) ? $_GET['rowsel'] : "";

	$compare = new COMPARE($srv, $srvdba, $srvdbb, QUIET);
	$obj = $compare->rowcompare($table, $rowsel);
	unset($compare); // Destroy object, otherwise output-buffer is in affect

	echo json_encode($obj);
	exit;
	}
// =================================================================================


// =================================================================================
//  Table Sync
// =================================================================================
if (isset($_GET['get']) && ($_GET['get']=='tablesync')) {

	$srvdba = (isset($_GET['dbfrom'])) ? $_GET['dbfrom'] : "";
	$srvdbb = (isset($_GET['dbto'])) ? $_GET['dbto'] : "";
	$table  = (isset($_GET['table'])) ? $_GET['table'] : "";
	$action = (isset($_GET['action'])) ? $_GET['action'] : "";
	
	$mySync = new SYNC($srv, $srvdba, $srvdbb, QUIET);
	//if (isset($_GET[' '])) $sync-> ($_GET[' ']); // Stel alternatieve in om keys te bepalen
	$obj = $mySync->doSync($table, $table, $action);
	unset($mySync); // Destroy object, otherwise output-buffer is in affect
	
	echo json_encode($obj);
	exit;
}
// =================================================================================


// =================================================================================
//  Backup, restore & drop actions
// =================================================================================
if (isset($_GET['get']) && ($_GET['get']=='backup')) {
	
	$id = (isset($_GET['id'])) ? ($_GET['id']) : null;
	$id = str_replace(array("'", "&", "|", ">", "<", "`", '"', '@'), "", $id); // TODO: Better safety before sending this $id as BASH command (restore);
	$db = (isset($_GET['db'])) ? $_GET['db'] : null;
	$action = substr($_GET['action'], 3, 3);
	
	$bck = new Backuprestore($srv, $setting, $db);
	$obj = $bck->execute($action, $id);
	unset($bck);
	
	echo json_encode($obj);
	exit;
}
// =================================================================================
